#include "ScoreClient.h"
#include "font.h"

#include <string>

#define CHAR_WIDTH 16.0

ScoreClient::ScoreClient() {
	points = 0;
	position = sf::Vector2f(30.0, 30.0);

	/* Initialisation des matrices */
	sf::Vector2f matriceTemp[10] = {
		sf::Vector2f(256.0, 0.0),
		sf::Vector2f(272.0, 0.0),
		sf::Vector2f(288.0, 0.0),
		sf::Vector2f(304.0, 0.0),
		sf::Vector2f(320.0, 0.0),
		sf::Vector2f(336.0, 0.0),
		sf::Vector2f(352.0, 0.0),
		sf::Vector2f(368.0, 0.0),
		sf::Vector2f(384.0, 0.0),
		sf::Vector2f(400.0, 0.0)
	};
	memcpy(numbersMatrice, matriceTemp, sizeof(sf::Vector2f) * 10);

	/* Initialisation des matrices */
	sf::Vector2f matriceTemp2[26] = {
		sf::Vector2f(528.0, 0.0),
		sf::Vector2f(544.0, 0.0),
		sf::Vector2f(560.0, 0.0),
		sf::Vector2f(576.0, 0.0),
		sf::Vector2f(592.0, 0.0),
		sf::Vector2f(608.0, 0.0),
		sf::Vector2f(624.0, 0.0),
		sf::Vector2f(640.0, 0.0),
		sf::Vector2f(656.0, 0.0),
		sf::Vector2f(672.0, 0.0),
		sf::Vector2f(688.0, 0.0),
		sf::Vector2f(704.0, 0.0),
		sf::Vector2f(720.0, 0.0),
		sf::Vector2f(736.0, 0.0),
		sf::Vector2f(752.0, 0.0),
		sf::Vector2f(768.0, 0.0),
		sf::Vector2f(784.0, 0.0),
		sf::Vector2f(800.0, 0.0),
		sf::Vector2f(816.0, 0.0),
		sf::Vector2f(832.0, 0.0),
		sf::Vector2f(848.0, 0.0),
		sf::Vector2f(864.0, 0.0),
		sf::Vector2f(880.0, 0.0),
		sf::Vector2f(896.0, 0.0),
		sf::Vector2f(912.0, 0.0),
		sf::Vector2f(928.0, 0.0)
	};
	memcpy(lettersMatrice, matriceTemp2, sizeof(sf::Vector2f) * 26);

	std::string path = "../Img/font/full_font.png";

	if (!fontTexture.loadFromFile(path)) {
		printf("Erreur au chargement de la texture\n");
	}
}

void ScoreClient::setScore(int newScore) {
	points = newScore;
}

void ScoreClient::drawScore(sf::RenderWindow& w) {
	sf::Vector2f posInter = position;

	sprite[0].setTexture(fontTexture, true);
	sprite[0].setTextureRect(sf::IntRect(lettersMatrice[S].x,
		lettersMatrice[S].y, CHAR_WIDTH, CHAR_WIDTH));
	sprite[0].setOrigin(CHAR_WIDTH/2, CHAR_WIDTH/2);
	sprite[0].setPosition(posInter);

	posInter.x = posInter.x + CHAR_WIDTH + 1.0;

	
	sprite[1].setTexture(fontTexture, true);
	sprite[1].setTextureRect(sf::IntRect(lettersMatrice[C].x,
		lettersMatrice[C].y, CHAR_WIDTH, CHAR_WIDTH));
	sprite[1].setOrigin(CHAR_WIDTH/2, CHAR_WIDTH/2);
	sprite[1].setPosition(posInter);

	posInter.x = posInter.x + CHAR_WIDTH + 1.0;


	sprite[2].setTexture(fontTexture, true);
	sprite[2].setTextureRect(sf::IntRect(lettersMatrice[O].x,
		lettersMatrice[O].y, CHAR_WIDTH, CHAR_WIDTH));
	sprite[2].setOrigin(CHAR_WIDTH/2, CHAR_WIDTH/2);
	sprite[2].setPosition(posInter);

	posInter.x = posInter.x + CHAR_WIDTH + 1.0;


	sprite[3].setTexture(fontTexture, true);
	sprite[3].setTextureRect(sf::IntRect(lettersMatrice[R].x,
		lettersMatrice[R].y, CHAR_WIDTH, CHAR_WIDTH));
	sprite[3].setOrigin(CHAR_WIDTH/2, CHAR_WIDTH/2);
	sprite[3].setPosition(posInter);

	posInter.x = posInter.x + CHAR_WIDTH + 1.0;


	sprite[4].setTexture(fontTexture, true);
	sprite[4].setTextureRect(sf::IntRect(lettersMatrice[E].x,
		lettersMatrice[E].y, CHAR_WIDTH, CHAR_WIDTH));
	sprite[4].setOrigin(CHAR_WIDTH/2, CHAR_WIDTH/2);
	sprite[4].setPosition(posInter);

	posInter.x = posInter.x + CHAR_WIDTH + 6.0;


	int score = points;
	int num1, num2, num3;
	num3 = score % 10;
	score /= 10;
	num2 = score % 10;
	score /= 10;
	num1 = score;

	if (num1 != 0) {
		sprite[5].setTexture(fontTexture, true);
		sprite[5].setTextureRect(sf::IntRect(numbersMatrice[num1].x,
			numbersMatrice[num1].y, CHAR_WIDTH, CHAR_WIDTH));
		sprite[5].setOrigin(CHAR_WIDTH/2, CHAR_WIDTH/2);
		sprite[5].setPosition(posInter);
	}
	posInter.x = posInter.x + CHAR_WIDTH + 1.0;

	if (num1 != 0 || num2 != 0) {
		sprite[6].setTexture(fontTexture, true);
		sprite[6].setTextureRect(sf::IntRect(numbersMatrice[num2].x,
			numbersMatrice[num2].y, CHAR_WIDTH, CHAR_WIDTH));
		sprite[6].setOrigin(CHAR_WIDTH/2, CHAR_WIDTH/2);
		sprite[6].setPosition(posInter);
	}
	posInter.x = posInter.x + CHAR_WIDTH + 1.0;

	sprite[7].setTexture(fontTexture, true);
	sprite[7].setTextureRect(sf::IntRect(numbersMatrice[num3].x,
		numbersMatrice[num3].y, CHAR_WIDTH, CHAR_WIDTH));
	sprite[7].setOrigin(CHAR_WIDTH/2, CHAR_WIDTH/2);
	sprite[7].setPosition(posInter);

	int i;
	for (i = 0; i < 8; i++) {
		w.draw(sprite[i]);
	}
}